package com.srd.phone.mvp.presenters;

import android.content.Intent;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.srd.phone.activity.StaticPageActivity;
import com.srd.phone.application.HttpStringRequest;
import com.srd.phone.mvp.interfaces.IBaseView;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegistPeoplePresenter extends BasePresenter {

    public RegistPeoplePresenter(RequestQueue requestQueue, IBaseView iView0) {
        super(requestQueue, iView0);
    }

    //登陆
    public void requestLogin(final String url, final String input, final String pwd, final String verify) {
        HttpStringRequest request = new HttpStringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                iView.get().returnData(url, response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                iView.get().onError(url, error);
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<>();
                map.put("input", input);
                map.put("pwd", pwd);
                map.put("vc", verify);
                return map;
            }
        };
        request.setTag(url);
        requestQueue.add(request);
    }


    //注册验证
    public String checkRegister(String user, String pwd, String check_pwd, String checkValue, boolean isAgree) {
        if (TextUtils.isEmpty(user)) {
            return "手机号码不能为空";
        } else if (TextUtils.isEmpty(pwd)) {
            return "输入密码不能为空";
        } else if (pwd.length() < 6) {
            return "密码个数不能少于6个";
        } else if (TextUtils.isEmpty(check_pwd)) {
            return "确认密码不能为空";
        } else if (TextUtils.isEmpty(checkValue)) {
            return "短信验证码不能为空";
        } else if (!checkPhone(user)) {
            return "手机号码不存在";
        } else if (!pwd.equals(check_pwd)) {
            return "两次密码不相同";
        } else if (isAgree == false) {
            return "请同意注册协议跟投标须知";
        }
        return "";
    }

    /**
     * 普通用户注册
     *
     * @param url
     * @param nm    昵称，可以为空
     * @param pwd   密码
     * @param em    邮箱
     * @param ph    手机号
     * @param rid   角色id，目前是可以为空
     * @param regid 站点id，先写上4，
     */
    public void requestData(final String url, final String nm, final String pwd, final String em, final String ph,
                            final String rid, final String regid, final String vc) {
        HttpStringRequest request = new HttpStringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                iView.get().returnData(url, response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                iView.get().onError(url, error);
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<>();
                map.put("nm", nm);
                map.put("pwd", pwd);
                map.put("em", em);
                map.put("ph", ph);
                map.put("rid", rid);
                map.put("regid", regid);
                map.put("vc", vc);
                return map;
            }
        };
        request.setTag(url);
        requestQueue.add(request);
    }

    /**
     * 第三方用户注册
     */
    public void requestOtherData(final String url, final String rid, final String regid, final String nm, final String tid) {
        HttpStringRequest request = new HttpStringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                iView.get().returnData(url, response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                iView.get().onError(url, error);
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<>();
                map.put("rid", rid);
                map.put("regid", regid);
                map.put("nm", nm);
                map.put("tid", tid);
                return map;
            }
        };
        request.setTag(url);
        requestQueue.add(request);
    }

    /**
     * 网络请求：短信验证码发送
     */
    public void requestMSMCode(final String url, final String value) {
        HttpStringRequest request = new HttpStringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                iView.get().returnData(url, response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                iView.get().onError(url, error);
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<>();
                map.put("value", value);
                return map;
            }
        };
        request.setTag(url);
        requestQueue.add(request);
    }

    /**
     * 验证邮箱
     */
    private boolean checkEmail(String email) {
        boolean flag = false;
        try {
            String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
            Pattern regex = Pattern.compile(check);
            Matcher matcher = regex.matcher(email);
            flag = matcher.matches();
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }

    /**
     * 验证手机号
     */
    public boolean checkPhone(String phone) {
        if (TextUtils.isEmpty(phone) || phone.length() != 11)
            return false;
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(phone);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    public void setProtocol(TextView tv) {
        tv.setText("同意");
        String value = "《盛融贷用户注册协议》";
        SpannableString sb = new SpannableString(value);
        ClickableSpan span = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Intent intent = new Intent(widget.getContext(), StaticPageActivity.class);
                intent.putExtra("flag", 3);
                widget.getContext().startActivity(intent);
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.parseColor("#1781DA"));
                ds.setUnderlineText(false);
                ds.clearShadowLayer();
            }
        };
        sb.setSpan(span, 0, value.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        tv.append(sb);
        tv.append("及");
        value = "《盛融贷投标须知》";
        sb = new SpannableString(value);
        span = new ClickableSpan() {
            @Override
            public void onClick(View widget) {

            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.parseColor("#1781DA"));
                ds.setUnderlineText(false);
                ds.clearShadowLayer();
            }
        };
        sb.setSpan(span, 0, value.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        tv.append(sb);
        tv.setHighlightColor(Color.TRANSPARENT);
        tv.setMovementMethod(LinkMovementMethod.getInstance());
    }
}
